<?php

// [ 计划回调:更新开奖位 ] 替换 PlanCallback.php
// use app\common\lib\PlanCallback;
// PlanCallback::run($no,$res);

namespace app\common\lib;
use think\Db;

class PlanCallback
{
    public static function run($no, $res)
    {
        $ret = Db::name('plan')->where('no',$no)->find();
        if ( !is_null($ret) ) {
            $v1  = intval($res[0]);
            $v2  = intval($res[1]);
            $v3  = intval($res[2]);
            $v4  = intval($res[3]);
            $v5  = intval($res[4]);

            $v1Hash = explode(',',$ret['v1_hash']);
            $v2Hash = explode(',',$ret['v2_hash']);
            $v3Hash = explode(',',$ret['v3_hash']);
            $v4Hash = explode(',',$ret['v4_hash']);
            $v5Hash = explode(',',$ret['v5_hash']);

            $v1Loc  = self::_fdLoc($v1,$v1Hash);
            $v2Loc  = self::_fdLoc($v2,$v2Hash);
            $v3Loc  = self::_fdLoc($v3,$v3Hash);
            $v4Loc  = self::_fdLoc($v4,$v4Hash);
            $v5Loc  = self::_fdLoc($v5,$v5Hash);

            $data['v1_loc'] = $v1Loc;
            $data['v2_loc'] = $v2Loc;
            $data['v3_loc'] = $v3Loc;
            $data['v4_loc'] = $v4Loc;
            $data['v5_loc'] = $v5Loc;
            Db::name('plan')->where('no',$no)->update($data);
        }
    }

    // 定位开奖位
    private static function _fdLoc($val, $res)
    {
        $sub = 0;
        foreach ($res as $k => $v) {
            if ($v == $val) {
                $sub = $k;
                break;
            }
        }
        return ++$sub;
    }
}
